package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.dto.SpuSeriesDTO;
import com.yami.shop.bean.model.SpuSeries;
import com.yami.shop.bean.model.SpuSeriesRelation;
import com.yami.shop.bean.vo.SpuSeriesRelationVO;
import com.yami.shop.bean.vo.SpuSeriesVO;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageUtil;
import com.yami.shop.dao.SpuSeriesMapper;
import com.yami.shop.dao.SpuSeriesRelationMapper;
import com.yami.shop.service.SpuSeriesRelationService;
import com.yami.shop.service.SpuSeriesService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SpuSeriesServiceImpl  extends ServiceImpl<SpuSeriesMapper, SpuSeries> implements SpuSeriesService {

    @Autowired
    private SpuSeriesMapper spuSeriesMapper;

    @Autowired
    private SpuSeriesRelationMapper spuSeriesRelationMapper;

    @Autowired
    private SpuSeriesRelationService spuSeriesRelationService;

    @Override
    public PageVO<SpuSeries> getSpuSeriesList(PageDTO pageDTO, String seriesName) {
        QueryWrapper<SpuSeries> spuSeriesQueryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isNotEmpty(seriesName)){
            spuSeriesQueryWrapper.like("series_name",seriesName);
        }
        spuSeriesQueryWrapper.orderByDesc("create_time");
        PageVO<SpuSeries> pageVO = PageUtil.doPage(pageDTO, () -> spuSeriesMapper.selectList(spuSeriesQueryWrapper));
        return pageVO;
    }

    @Override
    @Transactional
    public void deleteSpuSeries(int id) {
        int i = spuSeriesMapper.deleteById(id);
        if(i<=0){
            throw new YamiShopBindException("删除系列异常");
        }
        QueryWrapper<SpuSeriesRelation> spuSeriesRelationQueryWrapper = new QueryWrapper<>();
        spuSeriesRelationQueryWrapper.eq("series_id",id);
        spuSeriesRelationMapper.delete(spuSeriesRelationQueryWrapper);
    }

    @Override
    @Transactional
    public void insertSpuSeries(SpuSeriesDTO spuSeriesDTO) {
        SpuSeries spuSeries = new SpuSeries();
        spuSeries.setSeriesImg(spuSeriesDTO.getSeriesImg());
        spuSeries.setSeriesInfo(spuSeriesDTO.getSeriesInfo());
        spuSeries.setSeriesName(spuSeriesDTO.getSeriesName());
        spuSeries.setCreateTime(new Date());
        spuSeries.setUpdateTime(new Date());
        spuSeriesMapper.insert(spuSeries);
        if(ObjectUtils.isNotEmpty(spuSeriesDTO.getProductId())){
            String[] split = spuSeriesDTO.getProductId().split(",");
            for (String prodId:split) {
                SpuSeriesRelation infoByproductId = spuSeriesRelationService.getInfoByproductId(Integer.valueOf(prodId));
                if(ObjectUtils.isNotEmpty(infoByproductId)){
                    throw new YamiShopBindException("商品"+prodId+"已绑定系列");
                }
                SpuSeriesRelation spuSeriesRelation = new SpuSeriesRelation();
                spuSeriesRelation.setProductId(Integer.valueOf(prodId));
                spuSeriesRelation.setSeriesId(spuSeries.getId());
                spuSeriesRelation.setCreateTime(new Date());
                spuSeriesRelation.setUpdateTime(new Date());
                spuSeriesRelationMapper.insert(spuSeriesRelation);
            }
        }
    }

    @Override
    @Transactional
    public void updateSpuSeries(SpuSeriesDTO spuSeriesDTO) {
        SpuSeries spuSeries = new SpuSeries();
        spuSeries.setId(spuSeriesDTO.getId());
        spuSeries.setSeriesImg(spuSeriesDTO.getSeriesImg());
        spuSeries.setSeriesInfo(spuSeriesDTO.getSeriesInfo());
        spuSeries.setSeriesName(spuSeriesDTO.getSeriesName());
        spuSeries.setUpdateTime(new Date());
        spuSeriesMapper.updateById(spuSeries);
        if(ObjectUtils.isNotEmpty(spuSeriesDTO.getProductId())){
            QueryWrapper<SpuSeriesRelation> spuSeriesRelationQueryWrapper = new QueryWrapper<>();
            spuSeriesRelationQueryWrapper.eq("series_id",spuSeriesDTO.getId());
            spuSeriesRelationMapper.delete(spuSeriesRelationQueryWrapper);
            String[] split = spuSeriesDTO.getProductId().split(",");
            List<SpuSeriesRelation> prodList = new ArrayList<>();
            for (String prodId : split) {
                SpuSeriesRelation infoByproductId = spuSeriesRelationService.getInfoByproductId(Integer.valueOf(prodId));
                if(ObjectUtils.isNotEmpty(infoByproductId)){
                    throw new YamiShopBindException("商品"+prodId+"已绑定系列");
                }
                SpuSeriesRelation spuSeriesRelation = new SpuSeriesRelation();
                spuSeriesRelation.setProductId(Integer.valueOf(prodId));
                spuSeriesRelation.setSeriesId(spuSeriesDTO.getId());
                spuSeriesRelation.setCreateTime(new Date());
                spuSeriesRelation.setUpdateTime(new Date());
                prodList.add(spuSeriesRelation);
            }
            spuSeriesRelationService.saveBatch(prodList);
        }
    }

    @Override
    public SpuSeriesVO getSpuSeriesVOInfo(Integer id) {
        SpuSeriesVO spuSeriesVO = new SpuSeriesVO();
        SpuSeries spuSeries = spuSeriesMapper.selectById(id);
        spuSeriesVO.setSpuSeries(spuSeries);
        List<SpuSeriesRelationVO> spuSeriesRelationList = spuSeriesRelationMapper.getSpuSeriesRelationList(id);
        spuSeriesVO.setSpuSeriesRelationVOList(spuSeriesRelationList);
        return spuSeriesVO;
    }

    @Override
    public List<SpuSeriesRelationVO> getListBySeriesId(Integer SeriesId) {
        return spuSeriesRelationMapper.getSpuSeriesRelationList(SeriesId);
    }


}
